<?php
class Ziying_model extends Base_Model
{
    public function __construct()
    {
        $this->load->database();
    }

    /**
     * 获取自营项目列表
     * @param $data(array)
     * @return array
     *
     * */
    public function getList($data = array())
    {
        $cityids=array();
        //城市条件
        if(!empty($data['cityId']))
        {
        	$data['cityId']=trim($data['cityId']);
        	$data['cityId']=rtrim($data['cityId'],'|');
            //多城市
			if(stripos($data['cityId'], '|'))
			{
				$citys = explode('|',$data['cityId']);
			}
			//单一城市
			else
			{
				$citys = array($data['cityId']);
			}
			//找出包含所选城市的自营项目id
			$cityres=$this->db->select('zyId')
				        	  ->from('mod_ziying_city')
				        	  ->where_in('cityId', $citys)->get()->result_array();
			
		    if(!empty($cityres)){
		       foreach ($cityres as $v){
			      $cityids[]=$v['zyId'];
			   }
            }else{
               $cityids=array(0);
            }
		}
		//类型条件
        $typeids=array();
        if(!empty($data['type']))
        {
        	$typeres=$this->db->select('targetNid')
        	->from('tiyu_categorygl')
        	->where('categoryId',$data['type'])->get()->result_array();
		    if(!empty($typeres)){
               foreach ($typeres as $v){
				  $typeids[]=$v['targetNid'];
			   }
            }else{
               $typeids=array(0); 
            }
		}
      
        $this->db->select('	z.*,c.count');
        //获取评论总条数
        $this->db->join('(SELECT
			               COUNT(*) as count,
                           track_id
                        FROM
                            mod_track_comments
                        GROUP BY
                            track_id ) as c', 'z.id = c.track_id', 'left');
        $this->db->from('mod_ziying as z');
        //取有效线路
        $this->db->where('z.status','VALID');
        //去掉案例，取产品
        $this->db->where('z.zy_type','PRODUCT');
        //城市条件
        if(!empty($cityids))
        $this->db->where_in('z.id',$cityids);
        //类型条件
        if(!empty($typeids))
        $this->db->where_in('z.id',$typeids);
        //offset,limit条件
        if(!empty($data['limit']))
        {
        	$offset = !isset($data['offset'])?0:$data['offset'];
            $this->db->limit($offset, $data['limit']);
        }       
        //排序
        if(!empty($data['order']))
        {
            if($data['order'] == 1)
            {
                $this->db->order_by('priceTotal','asc');
            }elseif ($data['order'] == 2)
            {
            	$this->db->order_by('orders','desc');
            }elseif ($data['order'] == 3)
            {
            	$this->db->order_by('score','desc');
            }
        }
        $query = $this->db->get()->result_array();
        $items = array();
        if(!empty($query))
        {
            foreach($query as $v)
            {
                $item = array();
                $item['id'] = $v['id'];
                $item['name'] = $v['name'];
                $item['thumb'] = $this->img($v['thumb']);
                $item['orders'] = $v['orders'];
                $item['city'] =  $this->getCityBy($v['id']);
                $item['summary'] = $v['summary'];
                $item['priceTotal'] = '￥'.round(floatval($v['priceTotal'])/100,2);
                //评论数目
                $item['count'] = empty($v['count'])?0:$v['count'];
                //标签
                $v['tags'] = trim($v['tags']);
                $v['tags'] = ltrim($v['tags'],'|');
                $v['tags'] = rtrim($v['tags'],'|');
                $tags = explode('|', $v['tags']);
                foreach ($tags as &$tag){
                	$tag = trim($tag);
                }
                $item['tags'] = $tags; 
                
                $items[] = $item;
            }

        }
        return $items;
    }


    public function getCityBy($id)
    {
        $this->db->where('z.zyId', $id);
        $this->db->join('mod_city as city', 'city.id = z.cityId', 'left');
        $this->db->from('mod_ziying_city as z');
        $query = $this->db->get()->result_array();
        $city = array();
        foreach ($query as $k => $v) {
            $c = explode('|', $v['cityName']);
            $city[] = $c[false];
        }

        if (!empty($city))
        {
            return implode('|',$city);
        }else{
            return '';
        }

    }

    public function detail($id)
    {
        $this->db->where('id', $id);
        $this->db->from('mod_ziying');
        $query = $this->db->get()->row_array();
        //背景图
        $Imgs=trim($query['images']);
        $Imgs=rtrim($Imgs,"|");
        $Imgs = explode('|',$query['images']);
        if(!empty($Imgs))
        {
            foreach ($Imgs as $k=>$img)
            {
                $Imgs[$k] = $this->img($img);
            }

        }
        //ziying id
        $item['id']=$id;
        //city
        $cityids=trim($query["city"]);
        $cityids=rtrim($cityids,"|");
        $cityids= explode('|',$cityids);
        $citys=array();
        if(!empty($cityids))
        {
        	$citys=$this->getcitynamebyid($cityids, "CN");        	
        }
        $item['city']=$citys;
        //玩伴id，聊天对象
//         $item['rev_id']="1";
//         $item['name'] = $query['name'];
        $item['name'] = $query['name'];
        $item['summary'] = nl2br($query['summary']);
        $item['images'] = $Imgs;
        //$item['zan'] = $query['zan'];
        $item['price'] = "￥".round($query['priceTotal'] / 100, 2);
        $item['days'] = $query['days'];
        $item['orders'] = $query['orders'];
        $item['recommend'] = $query['recommend'];
        $item['freetimeDays'] = $query['freetimeDays'];
        $item['priceDetail'] = $query['priceDetail'];
        $item['personDetail'] = $query['personDetail'];
        return $item;

    }
    
    /**
     * 获取APP上自营项目行程介绍和价格介绍
     * @param $id
     * @return array
     *
     * */
    public function getDesc($id){
        $this->db->select('descLineApp,descPriceApp,descLineAppHeight,descLineAppWidth,descPriceAppHeight,descPriceAppWidth');
    	$this->db->where('id', $id);
    	$this->db->from('mod_ziying');
    	$query = $this->db->get()->row_array();
    	
    	if(empty($query)){
    		return '';
    	}
    	
    	//行程介绍图片
    	$lineimg=$query['descLineApp'];
    	$lineimg=trim($lineimg);
    	$lineimg=rtrim($lineimg,"|");
    	$lineimg = explode('|',$lineimg);
    	if(!empty($lineimg))
    	{
    		foreach ($lineimg as $img)
    		{
    			$lineimgres[] = $this->img($img);
    		}
    	
    	}
    	$item['descLineApp'] = $lineimgres;
    	
    	//价格详情图片
    	$priceimg=$query['descPriceApp'];
    	$priceimg=trim($priceimg);
    	$priceimg=rtrim($priceimg,"|");
    	$priceimg = explode('|',$priceimg);
    	if(!empty($priceimg))
    	{
    		foreach ($priceimg as $img)
    		{
    			$priceimgres[] = $this->img($img);
    		}
    	
    	}
    	$item['descPriceApp'] = $priceimgres;
    	$item['descLineAppHeight'] = $query['descLineAppHeight'];
    	$item['descLineAppWidth'] = $query['descLineAppWidth'];
    	$item['descPriceAppHeight'] = $query['descPriceAppHeight'];
    	$item['descPriceAppWidth'] = $query['descPriceAppWidth'];
    	
    	return $item;
    }
    
    /**
     * 获取自营案例的封面
     * @return array
     *
     * */
    public function caseCovers()
    {
    	$this->db->select('descProduct');
    	$this->db->from('mod_ziying');
    	$this->db->where(array('status'=>'VALID','zy_type'=>'CASE'));
    	$query = $this->db->get()->result_array();
    	
    	if (empty($query)) {
    		return '';
    	}
    	
    	foreach($query as $v){
    		$items['covers'][] = $this->img($v['descProduct']);
    	}
    	
    	return $items;
    }
    
    public function getcitynamebyid($cityid,$langue){
    	$this->db->select('cityName')->from('mod_city')->where_in("id",$cityid);
    	$query = $this->db->get()->result_array();
    	$name=array();
    	foreach ($query as $row){
    		$row["cityName"]=trim($row["cityName"]);
    		$row["cityName"]=rtrim($row["cityName"],"|");
    		$str = explode('|',$row["cityName"]);
    		$langue=strtoupper($langue);
    		switch ($langue){
    			case "CN":
    				$name[]=$str[0];
    				break;
    			case "EN":
    				$name[]=$str[1];
    				break;
    			default:
    				$name[]=$str[0];
    		}
    	}
    	return $name;   	
    }
    public function getbyid($id)
    {
    	$this->db->where('id', $id);
    	$this->db->from('mod_ziying');
    	$query = $this->db->get()->row_array();
        return $query;
    }

}
